/**
 * 
 */
package com.feib.soeasy.action;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;



import org.apache.commons.lang.StringUtils;

import com.feib.soeasy.model.Group;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.GroupService;
import com.feib.soeasy.service.Soez6001Service;
import com.feib.soeasy.service.TransferFeedbackService;
import com.feib.soeasy.util.UserUtil;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.DateTypeConverter;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationErrors;

/** 
 * 6001 超商/郵局撥款統計查詢
 * @author Jeff Gu
 *
 */
public class Soez6001ActionBean extends BaseActionBean {



	private static final String DEFAULT_JSP = "/WEB-INF/views/soez6001/soez6001.jsp";


	private static final String AJAX_LIST= "/WEB-INF/views/soez6001/soez6001List.jsp";

	private static final String AJAX_LIST_DETAIL = "/WEB-INF/views/soez6001/soez6001DetailList.jsp";

	/**
	 * 業者代號查詢畫面
	 */
	private static final String AJAX_QUERY_GROUP_JSP = "/WEB-INF/views/soez6001/soez6001groupPopUpList.jsp";


	/**
	 * 查詢條件
	 * 撥款日期 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFrom;

	/**
	 * 查詢條件
	 * 撥款日期 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateTo;


	/**
	 * 查詢條件
	 * 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFromD;

	/**
	 * 查詢條件
	 * 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateToD;

	/**
	 * 查詢條件
	 * 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFromE;

	/**
	 * 查詢條件
	 * 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateToE;

	/**
	 * 查詢條件
	 * 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFromF;

	/**
	 * 查詢條件
	 * 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateToF;

	/**
	 * 查詢條件
	 * 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFromG;

	/**
	 * 查詢條件
	 * 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateToG;

	/**
	 * 查詢條件
	 * 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFromH;

	/**
	 * 查詢條件
	 * 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateToH;

	/**
	 * 查詢條件
	 * 資料期間起
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateFromAll;

	/**
	 * 查詢條件
	 * 資料期間迄
	 */
	@Validate( converter=DateTypeConverter.class)
	private Date qryDateToAll;


	/**
	 *傳輸日期  起
	 */
	private Date qryDateMin;

	/**
	 *傳輸日期 訖
	 */
	private Date qryDateMax;

	/**
	 * 查詢條件
	 * 帳號
	 */
	private String qryAccountNo;

	/**
	 * 查詢條件
	 * 帳單業者
	 */
	private Group qryGroup;


	private String queryChannel;

	private List<String> queryChannels;

	private String channelD;
	private String channelE;
	private String channelF;
	private String channelG;
	private String channelH;

	/**
	 * 帳單業者查詢結果
	 */
	private PaginatedList<Group> queryGroupResult;

	/**
	 * 查詢結果
	 */
	private PaginatedList<Map<String, Object>> queryResult;


	/**
	 * 區分需要觀察的明細類型,以便調整jsp form需要帶入的條件匯出跟查詢
	 *detailType=2.單通路全
	 *detailType=3.全通路
	 */
	private String detailType;

	/**
	 * 群組類別<br>
	 * 1: Bank
	 * 2: 帳單業者-學校<br>
	 * 3: 帳單業者-管委會<br>
	 */

	private List<String> queryGroupType;


	/**
	 * 是否撥款
	 */
	private  String   transfered;


	/**
	 * 雜項代收
	 */
	private  String   groupN;


	/**
	 * 學雜費代收
	 */
	private  String   groupS;



	/**
	 *處理結果
	 */
	private String confirmMessage;



	@SpringBean("soez6001Service")
	private Soez6001Service soez6001Service;

	@SpringBean("groupService")
	private GroupService groupService;


	@SpringBean("transferFeedbackService")
	private TransferFeedbackService  transferFeedbackService;

	@DontBind
	@DefaultHandler
	public Resolution doDefault()
	{
		logger.debug("Enter doDefault mehtod.");
		try {
			Calendar cal = Calendar.getInstance();
			/* 初始化畫面查詢資料 */
			qryDateFrom=cal.getTime();
			qryDateTo=cal.getTime();

			qryDateToD = cal.getTime();
			qryDateFromD = cal.getTime();

			qryDateToE = cal.getTime();
			qryDateFromE = cal.getTime();

			qryDateToF = cal.getTime();
			qryDateFromF = cal.getTime();

			qryDateToG = cal.getTime();
			qryDateFromG = cal.getTime();

			qryDateToH = cal.getTime();
			qryDateFromH = cal.getTime();

			/* 全部勾選  */
			qryDateToAll = cal.getTime();
			qryDateFromAll = cal.getTime();

		}
		catch (Exception e) {
			logger.error("超商/郵局撥款統計查詢錯誤(Soez6001ActionBean.doDefault): {}",e);
		}
		return new ForwardResolution(DEFAULT_JSP); 
	}


	/**
	 * 查詢超商郵局已撥款資料
	 * @return
	 */
	public Resolution doAjaxQuery()
	{
		logger.debug("Enter doAjaxQuery mehtod.");
		try {

			Group	group=new Group();

			/* 畫面上有輸入卷商代號*/
			if(null!=qryGroup && StringUtils.isNotEmpty(qryGroup.getGroupNo())){
				group=qryGroup;
			}
			/* 限制卷商查詢 */
			if (!UserUtil.isBankGroup())
			{
				group=UserUtil.getUserGroup();
			}

			if (null == queryResult)
				queryResult = new PaginatedList<Map<String,Object>>();
			/* 顯示全部不分頁*/
			queryResult.setPageSize(-1);
			/* 只查郵局 */
			String[] channels = {channelD,channelE,channelF,channelG};	

			Date [] qryDateFroms = {qryDateFromD,qryDateFromE,qryDateFromF,qryDateFromG};

			Date[] qryDateTos ={qryDateToD,qryDateToE,qryDateToF,qryDateToG};

			compareDate(channels,qryDateFroms, qryDateTos );

			ArrayList<String> list_Channel = new ArrayList<String>();
			for (String channel : channels)
				if(!"null".equalsIgnoreCase(channel) && StringUtils.isNotBlank(channel) )
					list_Channel.add(channel);

			//不要去空白 後端會對不到
			//channels = list_Channel.toArray(new String[list_Channel.size()]);

			queryChannels=list_Channel;
			queryGroupType=new ArrayList<String>();

			/*雜項*/
			if(StringUtils.isNotBlank(groupN)){
				queryGroupType.add(Group.GROUP_TYPE_MANAGEMENT_COMMITTEE.toString());
				queryGroupType.add("4");
			}
			if(StringUtils.isNotBlank(groupS)){
				queryGroupType.add(Group.GROUP_TYPE_SCHOOL.toString());
			}

			/* 切換是否撥款*/
			if("0".equals(transfered)){
				queryResult = soez6001Service.queryNonTransferLogByGroup(group, channels, qryDateFroms, qryDateTos, queryGroupType, transfered, queryResult);

			}else if("1".equals(transfered)){
				queryResult = soez6001Service.queryTransferLogByGroup(group, channels, qryDateFroms, qryDateTos, queryGroupType, transfered, queryResult);
			}



		}
		catch (Exception e) {
			logger.error("超商/郵局撥款統計查詢錯誤(Soez6001ActionBean.doAjaxQuery): {}",e);
			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new LocalizableError("doAjaxQuery.exception") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);

		}


		return new ForwardResolution(AJAX_LIST); 
	}



	/**
	 * 超商郵局資料
	 * @return
	 */
	public Resolution doAjaxQueryPost()
	{
		logger.debug("Enter doAjaxQueryPost mehtod.");
		try {

			Group	group=new Group();

			/* 畫面上有輸入卷商代號*/
			if(null!=qryGroup && StringUtils.isNotEmpty(qryGroup.getGroupNo())){
				group=qryGroup;
			}
			/* 限制卷商查詢 */
			if (!UserUtil.isBankGroup())
			{
				group=UserUtil.getUserGroup();
			}

			if (null == queryResult)
				queryResult = new PaginatedList<Map<String,Object>>();
			/* 顯示全部不分頁*/
			queryResult.setPageSize(-1);
			/* 只查郵局 */
			String[] channels = {channelH};	

			Date [] qryDateFroms = {qryDateFromH};

			Date[] qryDateTos ={qryDateToH};
			this.qryDateMin=qryDateFromH;
			this.qryDateMax=qryDateToH;

			queryChannels= new ArrayList<String>();
			queryChannels.add(channelH);
			queryGroupType=new ArrayList<String>();



			/*雜項*/
			if(StringUtils.isNotBlank(groupN)){
				queryGroupType.add(Group.GROUP_TYPE_MANAGEMENT_COMMITTEE.toString());
				queryGroupType.add("4");
			}
			if(StringUtils.isNotBlank(groupS)){
				queryGroupType.add(Group.GROUP_TYPE_SCHOOL.toString());
			}


			if("0".equals(transfered)){
				queryResult = soez6001Service.queryNonTransferLogByGroup(group, channels, qryDateFroms, qryDateTos, queryGroupType, transfered, queryResult);

			}else if("1".equals(transfered)){
				queryResult = soez6001Service.queryTransferLogByGroup(group, channels, qryDateFroms, qryDateTos, queryGroupType, transfered, queryResult);
			}


		}
		catch (Exception e) {
			logger.error("超商/郵局撥款統計查詢錯誤(Soez6001ActionBean.doAjaxQueryPost): {}",e);
			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new LocalizableError("doAjaxQueryPost.exception") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);

		}
		return new ForwardResolution(AJAX_LIST); 
	}



	/**
	 * 根據通路跟日期 取得  已撥款明細 (可分群組)
	 * @author Jeff
	 */
	public Resolution doAjaxQueryTotalCommissionCode()
	{
		logger.debug("Enter doAjaxQueryTotalCommissionCode mehtod.");


		try {

			/* 不是帳單業者要加入限制 */
			Group	group=new Group();

			/* 畫面上有輸入卷商代號*/
			if(null!=qryGroup && StringUtils.isNotEmpty(qryGroup.getGroupNo())){
				group=qryGroup;
			}
			/* 限制卷商查詢 */
			if (!UserUtil.isBankGroup())
			{
				group=UserUtil.getUserGroup();
			}

			if (null == queryResult)
				queryResult = new PaginatedList<Map<String,Object>>();

			queryGroupType=new ArrayList<String>();
			/*雜項*/
			if(StringUtils.isNotBlank(groupN)){
				queryGroupType.add(Group.GROUP_TYPE_MANAGEMENT_COMMITTEE.toString());
				queryGroupType.add("4");
			}
			if(StringUtils.isNotBlank(groupS)){
				queryGroupType.add(Group.GROUP_TYPE_SCHOOL.toString());
			}

			/**
			 * 區分需要觀察的明細類型,以便調整jsp form需要帶入的條件匯出跟查詢
			 *detailType=2.單通路全代碼 ex: 7-11  全部代碼
			 *detailType=3.全通路全代碼 (畫面上的超商／郵局合計)ex: 7-11  okcvs
			 */

			if("3".equals(detailType)){


				String[] channels = {channelD,channelE,channelF,channelG,channelH};	


				Date [] qryDateFroms = 
				{
						qryDateFromD,qryDateFromE,qryDateFromF,qryDateFromG,qryDateFromH
				};

				Date[] qryDateTos =
				{
						qryDateToD,qryDateToE,qryDateToF,qryDateToG,qryDateToH
				};			


				queryResult = soez6001Service.queryGroupFeedbackLogByChannel(group, channels, qryDateFroms, qryDateTos, queryGroupType, transfered, qryAccountNo, queryResult);


			}else if("2".equals(detailType)){

				String[] channels = {queryChannel};


				if("0".equals(transfered)){

					/*切換日期為查詢區間*/
					changeDate(queryChannel);					
					Date [] qryDateFroms = {qryDateFrom};
					Date[] qryDateTos ={qryDateTo};

					queryResult = soez6001Service.queryGroupFeedbackLogByChannel(group, channels, qryDateFroms, qryDateTos, queryGroupType, transfered, qryAccountNo, queryResult);

				}else if("1".equals(transfered)){
					Date [] qryDateFroms = {qryDateFrom};
					Date[] qryDateTos ={qryDateTo};
					queryResult = soez6001Service.queryGroupFeedbackLogByChannel(group, channels, qryDateFroms, qryDateTos, queryGroupType, transfered, qryAccountNo, queryResult);
				}
			}



		}
		catch (Exception e) {
			logger.error("超商代收統計查詢錯誤(Soez6001ActionBean.doAjaxQueryTotalCommissionCode): {}",e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new LocalizableError("doAjaxQueryTotalCommissionCode.exception") );
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);

		}
		return new ForwardResolution(AJAX_LIST_DETAIL); 

	}



	/**
	 * 下載畫面Excel
	 * @return
	 */
	public Resolution doAjaxDownloadExcelFile()
	{
		logger.debug("Enter doAjaxDownloadExcelFile mehtod.");
		try {
			Group	group=new Group();

			/* 畫面上有輸入卷商代號*/
			if(null!=qryGroup && StringUtils.isNotEmpty(qryGroup.getGroupNo())){
				group=qryGroup;
			}
			/* 限制卷商查詢 */
			if (!UserUtil.isBankGroup())
			{
				group=UserUtil.getUserGroup();
			}

			if (null == queryResult)
				queryResult = new PaginatedList<Map<String,Object>>();
			/* 單通路單代碼明細資料保持顯示全部不分頁*/
			queryResult.setPageSize(-1);

			String[] channels = {channelD,channelE,channelF,channelG,channelH};	

			Date [] qryDateFroms = 
			{
					qryDateFromD,qryDateFromE,qryDateFromF,qryDateFromG,qryDateFromH
			};

			Date[] qryDateTos =
			{
					qryDateToD,qryDateToE,qryDateToF,qryDateToG,qryDateToH
			};

			queryGroupType=new ArrayList<String>();

			/*雜項*/
			if(StringUtils.isNotBlank(groupN)){
				queryGroupType.add(Group.GROUP_TYPE_MANAGEMENT_COMMITTEE.toString());
				queryGroupType.add("4");
			}
			if(StringUtils.isNotBlank(groupS)){
				queryGroupType.add(Group.GROUP_TYPE_SCHOOL.toString());
			}

			String downloadFilePath=null;
			/* 切換是否撥款*/
			if("0".equals(transfered)){
				downloadFilePath = soez6001Service.doExportNonTransferStatisticsXls(group, channels, qryDateFroms, qryDateTos, queryGroupType, transfered);

			}else if("1".equals(transfered)){
				downloadFilePath = this.soez6001Service.doExportGroupStatisticsXls(group,channels,  qryDateFroms , qryDateTos, queryGroupType, transfered);	
			}

			if (null == downloadFilePath)
			{
				// 查無資料
			}

			File downloadFile = new File(downloadFilePath);
			byte[] bytes = new byte[(int)downloadFile.length()];

			BufferedInputStream bufferedInputStream = 
				new BufferedInputStream(new FileInputStream(downloadFile)); 

			bufferedInputStream.read(bytes);

			ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
			inputStream.close();
			bufferedInputStream.close();

			String isoFileName = new String("超商郵局撥款統計表.xls".getBytes("MS950"), "ISO8859-1");

			StreamingResolution streamingResolution = new StreamingResolution( 
					"application/vnd.ms-excel",inputStream).setFilename(isoFileName);

			return streamingResolution;
		}
		catch (Exception e) {
			logger.error("", e);
		}
		return new ForwardResolution(DEFAULT_JSP); 
	}




	/**
	 * 下載明細excel
	 * @author Jeff Gu
	 * @return
	 */
	public Resolution doDownloadDetailExcelFile()
	{
		logger.debug("Enter doDownloadDetailExcelFile mehtod.");
		try {


			String downloadFilePath=null;

			/* 不是帳單業者要加入限制 */
			Group	group=new Group();

			/* 畫面上有輸入卷商代號*/
			if(null!=qryGroup && StringUtils.isNotEmpty(qryGroup.getGroupNo())){
				group=qryGroup;
			}
			/* 限制卷商查詢 */
			if (!UserUtil.isBankGroup())
			{
				group=UserUtil.getUserGroup();
			}

			queryGroupType=new ArrayList<String>();
			/*雜項*/
			if(StringUtils.isNotBlank(groupN)){
				queryGroupType.add(Group.GROUP_TYPE_MANAGEMENT_COMMITTEE.toString());
				queryGroupType.add("4");
			}
			if(StringUtils.isNotBlank(groupS)){
				queryGroupType.add(Group.GROUP_TYPE_SCHOOL.toString());
			}

			if("3".equals(detailType)){
				String[] channels = {channelD,channelE,channelF,channelG,channelH};	


				Date [] qryDateFroms = 
				{
						qryDateFromD,qryDateFromE,qryDateFromF,qryDateFromG,qryDateFromH
				};

				Date[] qryDateTos =
				{
						qryDateToD,qryDateToE,qryDateToF,qryDateToG,qryDateToH
				};	


				downloadFilePath = this.soez6001Service.doExportGroupDetailStatisticsXls(group,channels,  qryDateFroms , qryDateTos, queryGroupType, qryAccountNo, transfered);

			}else if("2".equals(detailType)){

				String[] channels = {queryChannel};

				if("0".equals(transfered)){
					/*切換日期為查詢區間*/
					changeDate(queryChannel);					
					Date [] qryDateFroms = {qryDateFrom};
					Date[] qryDateTos ={qryDateTo};

					downloadFilePath = this.soez6001Service.doExportGroupDetailStatisticsXls(group,channels,  qryDateFroms , qryDateTos, queryGroupType, qryAccountNo, transfered);

				}else if("1".equals(transfered)){
					Date [] qryDateFroms = {qryDateFrom};

					Date[] qryDateTos ={qryDateTo};

					downloadFilePath = this.soez6001Service.doExportGroupDetailStatisticsXls(group,channels,  qryDateFroms , qryDateTos, queryGroupType, qryAccountNo, transfered);
				}
			}



			if (null == downloadFilePath)
			{
				// 查無資料
			}

			File downloadFile = new File(downloadFilePath);
			byte[] bytes = new byte[(int)downloadFile.length()];

			BufferedInputStream bufferedInputStream = 
				new BufferedInputStream(new FileInputStream(downloadFile)); 

			bufferedInputStream.read(bytes);

			ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
			inputStream.close();
			bufferedInputStream.close();

			String isoFileName = new String("超商郵局撥款統計明細表.xls".getBytes("MS950"), "ISO8859-1");

			StreamingResolution streamingResolution = new StreamingResolution( 
					"application/vnd.ms-excel",inputStream).setFilename(isoFileName);

			return streamingResolution;
		}
		catch (Exception e) {
			logger.error("", e);
		}
		return new ForwardResolution(DEFAULT_JSP); 
	}



	/**
	 * 帳單業者查詢
	 * @return
	 */
	public Resolution doAjaxQueryGroups()
	{
		logger.debug("Enter doAjaxQueryGroups mehtod.");
		try {
			//如果不是銀行端，則查詢條件要加入帳單業者限制
			if (!UserUtil.isBankGroup())
			{
				throw new Exception("No Authority");
			}


			//USER未輸入查詢條件，所以查詢物件會是NULL(查詢物件不可為NULL)
			if (null == qryGroup)
				qryGroup = new Group();

			//呼叫服務查詢
			queryGroupResult = groupService.queryByGroupExcludeBank(qryGroup, queryGroupResult);
		}
		catch (Exception e) {
			logger.error("帳務處理統計表-帳單業者查詢錯誤(Soez5004ActionBean.doAjaxQueryGroups):", e);

			// 轉換錯誤訊息
			ValidationErrors errors = new ValidationErrors();
			errors.addGlobalError(new SimpleError("查詢錯誤！"));
			getContext().setValidationErrors(errors);
			// 回傳 錯誤 HTML fragments
			return super.errorsToHtmlStreams(errors);
		}

		return new ForwardResolution(AJAX_QUERY_GROUP_JSP);
	}




	/**
	 * 撥款按鈕
	 * @return
	 */
	public Resolution doTransferFeedback()
	{
		logger.debug("Enter doTransferFeedback mehtod.");
		try {

			Group	group=new Group();

			/* 畫面上有輸入卷商代號*/
			if(null!=qryGroup && StringUtils.isNotEmpty(qryGroup.getGroupNo())){
				group=qryGroup;
			}
			/* 限制卷商查詢 */
			if (!UserUtil.isBankGroup())
			{
				group=UserUtil.getUserGroup();
			}

			if (null == queryResult)
				queryResult = new PaginatedList<Map<String,Object>>();
			/* 單通路單代碼明細資料保持顯示全部不分頁*/
			queryResult.setPageSize(-1);

			String[] channels = {channelD,channelE,channelF,channelG,channelH};	

			Date [] qryDateFroms = 
			{
					qryDateFromD,qryDateFromE,qryDateFromF,qryDateFromG,qryDateFromH
			};

			Date[] qryDateTos =
			{
					qryDateToD,qryDateToE,qryDateToF,qryDateToG,qryDateToH
			};

			queryGroupType=new ArrayList<String>();

			/*雜項*/
			if(StringUtils.isNotBlank(groupN)){
				queryGroupType.add(Group.GROUP_TYPE_MANAGEMENT_COMMITTEE.toString());
				queryGroupType.add("4");
			}
			if(StringUtils.isNotBlank(groupS)){
				queryGroupType.add(Group.GROUP_TYPE_SCHOOL.toString());
			}
			transferFeedbackService.doTransferFeedbackLog(group, channels, qryDateFroms, qryDateTos, queryGroupType);
			//檢查畫面這是要幹嘛 要reload
			confirmMessage="撥款成功,請查詢已撥款資料下載撥款檔";

		} catch (Exception e) {
			confirmMessage="撥款失敗";
			logger.error("人工撥款作業錯誤(Soez6001ActionBean.doTransferFeedback):", e);
		}
		  StreamingResolution resolution = new StreamingResolution("text/html;charset=utf-8", new StringReader(confirmMessage));
	      resolution.setCharacterEncoding("UTF-8");
	      return resolution; 
	}





	/**
	 * 下載撥款檔
	 * @author Jeff Gu
	 * @return
	 */
	public Resolution doDownloadTransferFeedbackFile()
	{
		logger.debug("Enter doDownloadTransferFeedbackFile mehtod.");
		try {


			String downloadFilePath=null;

			String[] channels = {channelD,channelE,channelF,channelG,channelH};	
			
			Date [] qryDateFroms = 
			{
					qryDateFromD,qryDateFromE,qryDateFromF,qryDateFromG,qryDateFromH
			};

			Date[] qryDateTos =
			{
					qryDateToD,qryDateToE,qryDateToF,qryDateToG,qryDateToH
			};	
			
			
			this.compareDate(channels, qryDateFroms, qryDateTos);
			
			int transferType=1;
			if(StringUtils.isNotBlank(channelH)){
				transferType=2;
			}
			
			downloadFilePath=transferFeedbackService.makeTransferMediaFile(qryDateMax, transferType);

			if (null == downloadFilePath)
			{
				// 查無資料
			}

			File downloadFile = new File(downloadFilePath);
			byte[] bytes = new byte[(int)downloadFile.length()];

			BufferedInputStream bufferedInputStream = 
				new BufferedInputStream(new FileInputStream(downloadFile)); 

			bufferedInputStream.read(bytes);

			ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
			inputStream.close();
			bufferedInputStream.close();

			StreamingResolution streamingResolution;
			
			if(StringUtils.isNotBlank(channelH)){
				 streamingResolution = new StreamingResolution( 
							"application/txt",inputStream).setFilename(downloadFile.getName());
			}else{
				 streamingResolution = new StreamingResolution( 
							"application/zip",inputStream).setFilename(downloadFile.getName());
		
			}
					

			return streamingResolution;
		}
		catch (Exception e) {
			logger.error("", e);
		}
		return new ForwardResolution(DEFAULT_JSP); 
	}







	/**
	 * 產生畫面傳輸日期區間,並去除跳號
	 * @param qryDateFroms
	 * @param qryDateTos
	 */
	private void compareDate(String[] channels,Date [] qryDateFroms, Date [] qryDateTos )
	{

		qryDateMin=qryDateFroms[0];
		qryDateMax=qryDateTos[0];
		for(int i=1;i<qryDateFroms.length;i++)
		{			
			Date dateFrom = qryDateFroms[i];
			String channel = channels[i];
			if(StringUtils.isNotBlank(channel))
			{
				if(null!= dateFrom){
					if(null==qryDateMin||dateFrom.before(qryDateMin))
						qryDateMin=dateFrom;	
				}
			}

		}	

		for(int i=1;i<qryDateTos.length;i++)
		{			
			Date dateTo = qryDateTos[i];
			String channel = channels[i];
			if(StringUtils.isNotBlank(channel))
			{
				if(null!= dateTo){
					if(null==qryDateMax||dateTo.after(qryDateMax))
						qryDateMax=dateTo;	
				}
			}
		}

	}


	/**
	 * 切換日期為查詢區間
	 * @param queryChannel
	 */
	private void  changeDate(String queryChannel){

		if("D".equals(queryChannel)){
			qryDateFrom=qryDateFromD;
			qryDateTo=qryDateToD;
		}else if("E".equals(queryChannel)){
			qryDateFrom=qryDateFromE;
			qryDateTo=qryDateToE;
		}else if("F".equals(queryChannel)){
			qryDateFrom=qryDateFromF;
			qryDateTo=qryDateToF;
		}else if("G".equals(queryChannel)){
			qryDateFrom=qryDateFromG;
			qryDateTo=qryDateToG;
		}else if("H".equals(queryChannel)){
			qryDateFrom=qryDateFromH;
			qryDateTo=qryDateToH;
		}


	}


	public Date getQryDateFrom() {
		return qryDateFrom;
	}

	public void setQryDateFrom(Date qryDateFrom) {
		this.qryDateFrom = qryDateFrom;
	}

	public Date getQryDateTo() {
		return qryDateTo;
	}

	public void setQryDateTo(Date qryDateTo) {
		this.qryDateTo = qryDateTo;
	}


	public Group getQryGroup() {
		return qryGroup;
	}


	public void setQryGroup(Group qryGroup) {
		this.qryGroup = qryGroup;
	}


	public PaginatedList<Group> getQueryGroupResult() {
		return queryGroupResult;
	}


	public void setQueryGroupResult(PaginatedList<Group> queryGroupResult) {
		this.queryGroupResult = queryGroupResult;
	}


	public PaginatedList<Map<String, Object>> getQueryResult() {
		return queryResult;
	}


	public void setQueryResult(PaginatedList<Map<String, Object>> queryResult) {
		this.queryResult = queryResult;
	}


	public String getQueryChannel() {
		return queryChannel;
	}


	public void setQueryChannel(String queryChannel) {
		this.queryChannel = queryChannel;
	}


	public String getDetailType() {
		return detailType;
	}


	public void setDetailType(String detailType) {
		this.detailType = detailType;
	}


	public Date getQryDateFromD() {
		return qryDateFromD;
	}


	public void setQryDateFromD(Date qryDateFromD) {
		this.qryDateFromD = qryDateFromD;
	}


	public Date getQryDateToD() {
		return qryDateToD;
	}


	public void setQryDateToD(Date qryDateToD) {
		this.qryDateToD = qryDateToD;
	}


	public Date getQryDateFromE() {
		return qryDateFromE;
	}


	public void setQryDateFromE(Date qryDateFromE) {
		this.qryDateFromE = qryDateFromE;
	}


	public Date getQryDateToE() {
		return qryDateToE;
	}


	public void setQryDateToE(Date qryDateToE) {
		this.qryDateToE = qryDateToE;
	}


	public Date getQryDateFromF() {
		return qryDateFromF;
	}


	public void setQryDateFromF(Date qryDateFromF) {
		this.qryDateFromF = qryDateFromF;
	}


	public Date getQryDateToF() {
		return qryDateToF;
	}


	public void setQryDateToF(Date qryDateToF) {
		this.qryDateToF = qryDateToF;
	}


	public Date getQryDateFromG() {
		return qryDateFromG;
	}


	public void setQryDateFromG(Date qryDateFromG) {
		this.qryDateFromG = qryDateFromG;
	}


	public Date getQryDateToG() {
		return qryDateToG;
	}


	public void setQryDateToG(Date qryDateToG) {
		this.qryDateToG = qryDateToG;
	}


	public Date getQryDateFromH() {
		return qryDateFromH;
	}


	public void setQryDateFromH(Date qryDateFromH) {
		this.qryDateFromH = qryDateFromH;
	}


	public Date getQryDateToH() {
		return qryDateToH;
	}


	public void setQryDateToH(Date qryDateToH) {
		this.qryDateToH = qryDateToH;
	}


	public Date getQryDateFromAll() {
		return qryDateFromAll;
	}


	public void setQryDateFromAll(Date qryDateFromAll) {
		this.qryDateFromAll = qryDateFromAll;
	}


	public Date getQryDateToAll() {
		return qryDateToAll;
	}


	public void setQryDateToAll(Date qryDateToAll) {
		this.qryDateToAll = qryDateToAll;
	}


	public String getChannelD() {
		return channelD;
	}


	public void setChannelD(String channelD) {
		this.channelD = channelD;
	}


	public String getChannelE() {
		return channelE;
	}


	public void setChannelE(String channelE) {
		this.channelE = channelE;
	}


	public String getChannelF() {
		return channelF;
	}


	public void setChannelF(String channelF) {
		this.channelF = channelF;
	}


	public String getChannelG() {
		return channelG;
	}


	public void setChannelG(String channelG) {
		this.channelG = channelG;
	}


	public String getChannelH() {
		return channelH;
	}


	public void setChannelH(String channelH) {
		this.channelH = channelH;
	}


	public GroupService getGroupService() {
		return groupService;
	}


	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}


	public List<String> getGueryGroupType() {
		return queryGroupType;
	}


	public void setGueryGroupType(List<String> gueryGroupType) {
		this.queryGroupType = gueryGroupType;
	}


	public String getTransfered() {
		return transfered;
	}


	public void setTransfered(String transfered) {
		this.transfered = transfered;
	}


	public String getGroupN() {
		return groupN;
	}


	public void setGroupN(String groupN) {
		this.groupN = groupN;
	}


	public String getGroupS() {
		return groupS;
	}


	public void setGroupS(String groupS) {
		this.groupS = groupS;
	}


	public List<String> getQueryChannels() {
		return queryChannels;
	}


	public void setQueryChannels(List<String> queryChannels) {
		this.queryChannels = queryChannels;
	}


	public List<String> getQueryGroupType() {
		return queryGroupType;
	}


	public void setQueryGroupType(List<String> queryGroupType) {
		this.queryGroupType = queryGroupType;
	}


	public Date getQryDateMin() {
		return qryDateMin;
	}


	public void setQryDateMin(Date qryDateMin) {
		this.qryDateMin = qryDateMin;
	}


	public Date getQryDateMax() {
		return qryDateMax;
	}


	public void setQryDateMax(Date qryDateMax) {
		this.qryDateMax = qryDateMax;
	}


	public String getQryAccountNo() {
		return qryAccountNo;
	}


	public void setQryAccountNo(String qryAccountNo) {
		this.qryAccountNo = qryAccountNo;
	}


	public String getConfirmMessage() {
		return confirmMessage;
	}


	public void setConfirmMessage(String confirmMessage) {
		this.confirmMessage = confirmMessage;
	}










}
